package cn.software.controller;

import cn.software.anntation.LogAnnotation;
import cn.software.entity.Topic;
import cn.software.entity.param.TopicParam;
import cn.software.entity.vo.TopicVo;
import cn.software.service.TopicService;
import cn.software.util.Result;
import cn.software.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/topic")
//@Api(tags = "题目接口")
public class TopicController {

    @Autowired
    private TopicService topicService;

    @PostMapping("/addTopic")
    @LogAnnotation(moudle = "题目", operator = "添加题目")
    //@ApiOperation("增加题目")
    public Result addTopic(@RequestBody TopicParam topicParam) {
        boolean isSuccess = topicService.addTopic(topicParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("新增失败");
        }
    }

    @GetMapping("/listTopic")
    @LogAnnotation(moudle = "题目", operator = "根据科目id获取题目")
    public Result listTopic(@RequestParam("subjectId") Integer subjectId) {
        List<TopicVo> topicVoList = topicService.listTopic(subjectId);
        if(topicVoList == null) {
            return Result.fail(ResultEnum.FAIL_SUBJECT_NOT_EXIST);
        } else {
            return Result.success(topicVoList);
        }
    }

    @GetMapping("/listAllTopic")
    @LogAnnotation(moudle = "题目", operator = "获取所有题目")
    public Result listAllTopic(@RequestParam("curPage") Integer curPage) {
        List<TopicVo> topicVoList =  topicService.listAllTopic(curPage);
        return Result.success(topicVoList);
    }

    @PostMapping("/editTopic")
    @LogAnnotation(moudle = "题目", operator = "编辑题目")
    public Result editTopic(@RequestBody TopicParam topicParam) {
        boolean isSuccess = topicService.editTopic(topicParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @PostMapping("/deleteTopic")
    @LogAnnotation(moudle = "题目", operator = "删除题目")
    public Result deleteTopic(@RequestParam("topicId") Integer topicId) {
        boolean isSuccess = topicService.deleteTopic(topicId);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("删除失败");
        }
    }


    @GetMapping("/getTopic")
    @LogAnnotation(moudle = "题目", operator = "根据题目id获取题目")
    public Result getTopic(@RequestParam("id") Integer id) {
        TopicVo topicVo = topicService.getTopic(id);
        if(topicVo == null) {
            return Result.fail("题目不存在");
        } else {
            return Result.success(topicVo);
        }
    }
}
